<?php

namespace App\Http\Resources\Stats\User\Player;

use App\Http\Resources\Resource;
use Helper\Timed;

class MonthResource extends Resource
{

	public function toArray($request)
	{
		$charts = [
			'timed' => [],
			'time' => [],
			'data' => [
				'u_game' => [],
				'u_sign' => [],
				'nu_game' => [],
				'nu_sign' => [],
			],
		];

		$datetime = $request->input('datetime');

		date_default_timezone_set(Timed::getJsLocalTimezoneNameByCookie());
		$start_time = strtotime(date('Y-m-01', $datetime[0]));
		$end_time = strtotime(date('Y-m-01', strtotime('+1 month', $datetime[1])));

		list($timed, $timedEnd) = Timed::datetimeFormat($request, 'datetime', $start_time, $end_time - 1);

		date_default_timezone_set(Timed::getTimezoneName());

		$stats = $this->resource['stats'];
		foreach ($stats['u_game'] as $k => $v) {
			$stats['u_game'][date('Y.m', $k)] = $v;
		}
		foreach ($stats['nu_game'] as $k => $v) {
			$stats['nu_game'][date('Y.m', $k)] = $v;
		}
		foreach ($stats['nu_sign'] as $k => $v) {
			$stats['nu_sign'][date('Y.m', $k)] = $v;
		}

		for (; $timed <= $timedEnd; $timed = Timed::getMonthNextTimed($timed)) {
			$month = date('Y.m', $timed);
			if ($month > date('Y.m')) {
				continue;
			}
			$charts['timed'][] = date('Y.m', $timed);
			$charts['time'][] = $timed;
			$charts['data']['u_game'][] = $stats['u_game'][$month] ?? null;
			$charts['data']['u_sign'][] = $stats['u_sign'][$month] ?? null;
			$charts['data']['nu_game'][] = $stats['nu_game'][$month] ?? null;
			$charts['data']['nu_sign'][] = $stats['nu_sign'][$month] ?? null;
		}

		return $this->getResponse()->setResponseData($charts);
	}
    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    // public function toArray($request)
    // {
    //     $charts = [
    //         'timed' => [],
    //         'data' => [
    //             'u_game' => [],
    //             'u_sign' => [],
    //             'nu_game' => [],
    //             'nu_sign' => [],
    //         ],
    //     ];
	//
	//     $datetime = $request->input('datetime');
	//
	//     date_default_timezone_set(Timed::getJsLocalTimezoneNameByCookie());
	//     $start_time = strtotime(date('Y-m-01', $datetime[0]));
	//     $end_time = strtotime(date('Y-m-01', strtotime('+1 month', $datetime[1])));
	//     list($timed, $timedEnd) = Timed::datetimeFormat($request, 'datetime', $start_time, $end_time - 1);
    //     $stats = $this->resource['stats'];
	//
    //     $offset = Timed::getTimeZoneOffset(Timed::getTimezoneName());
	//
    //     for (; $timed <= $timedEnd; $timed = Timed::getMonthNextTimedTimeZone($timed, $offset)) {
	//         $date = date('Y-m', $timed);
    //         $charts['timed'][] = date('Y.m', $timed);
    //         // $charts['data']['u_game'][] = $stats[$timed]['u_game'] ?? null;
    //         // $charts['data']['u_sign'][] = $stats[$timed]['u_sign'] ?? null;
    //         // $charts['data']['nu_game'][] = $stats[$timed]['nu_game'] ?? null;
    //         // $charts['data']['nu_sign'][] = $stats[$timed]['nu_sign'] ?? null;
	//         $charts['data']['u_game'][] = $stats['u_game'][$date]['total'] ?? null;
	//         $charts['data']['u_sign'][] = $stats['u_sign'][$date]['total'] ?? null;
	//         $charts['data']['nu_game'][] = $stats['nu_game'][$date]['total'] ?? null;
	//         $charts['data']['nu_sign'][] = $stats['nu_sign'][$date]['total']  ?? null;
    //     }
	//
    //     return $this->getResponse()->setResponseData($charts);
    // }
}
